package com.clouddot.baipai.ui.fragment.H_MainFragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.model.beam.getCourseTable.getCourseTableResponse;
import com.clouddot.baipai.ui.fragment.BaseFragment;
import com.clouddot.baipai.ui.util.tableview.TableView;
import com.clouddot.baipai.ui.util.tableview.common.TableCellData;
import com.clouddot.baipai.ui.util.tableview.common.TableHeaderColumnModel;
import com.clouddot.baipai.ui.util.tableview.toolkits.SimpleTableDataAdapter;
import com.clouddot.baipai.ui.util.tableview.toolkits.SimpleTableHeaderAdapter;
import com.clouddot.baipai.ui.util.ToastUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2018/4/10.
 */

public class H_TableableFragment extends BaseFragment {

    private View view;
    private Context context;
    private ConstraintLayout constraintLayout;
    private RelativeLayout relativeLayout;
    private TableView tableView;
    private Map<Integer, Pair<String,Integer>> columns;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return view;
    }

    @Override
    protected void setContentView() {
        super.setContentView();

        view = LayoutInflater.from(context).inflate(R.layout.h_f_tableablefg,null);
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void initControls() {
        super.initControls();

        constraintLayout = view.findViewById(R.id.content);

        relativeLayout = view.findViewById(R.id.loading);

        constraintLayout.startAnimation(animation3);

        tableView = view.findViewById(R.id.tableview);
        ImageView loading = view.findViewById(R.id.imgloading);
        loading.startAnimation(loadingAnim);

    }

    @Override
    protected void setListener() {
        super.setListener();
    }

    @Override
    protected void loadParams() {
        super.loadParams();
    }

    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(Env.getAppContext().isInitial){
            relativeLayout.setVisibility(View.VISIBLE);
            tableView.setVisibility(View.GONE);
            runThreadAction();
        }
    }

    @Override
    public void runThreadAction() {
        super.runThreadAction();
    }

    @Override
    protected Object runThreadActionExceute() {
         super.runThreadActionExceute();
        return Env.getAppPresenter().getcourseTable(Env.getAppContext().classId);

    }

    @Override
    protected void runThreadActionSuccess(Object result) {
        super.runThreadActionSuccess(result);
        relativeLayout.setVisibility(View.GONE);
        getCourseTableResponse response = (getCourseTableResponse) result;
        if(response.isSuccess()){
            drawTable(response);

        }else {
            ToastUtil.showToast(context,response.getErrMsg());
        }
    }

    @Override
    protected void runThreadActionError(Exception ex) {
        super.runThreadActionError(ex);
    }

    private void initData(){
        columns = new LinkedHashMap<>();
        columns.put(0,new Pair<>("",7));
    }

    private List<TableCellData> getTableData(getCourseTableResponse response) {

        List<TableCellData> cellDatas = new ArrayList<>();
        cellDatas.add(new TableCellData("", 0, 0));
        cellDatas.add(new TableCellData("时间", 0, 1));
        int start = 0 ;
        int end = 0 ;

        //课程日期
        for (int i = 0 ;i < response.weeksNames.size();i++){
            cellDatas.add(new TableCellData(response.weeksNames.get(i).getName(), 0, i+2));
        }
        //课程时间
        for (int j = 0 ;j < response.classTimes.size() ;j ++){
            if(!response.classTimes.get(0).getBeginTime().equals("")){
                String [] str =  response.classTimes.get(j).getBeginTime().split(":");
                if(str.length > 0) {
                    if (Integer.parseInt(str[0]) > 7 && Integer.parseInt(str[0]) < 12) {
                        start++;
                    }
                    if (Integer.parseInt(str[0]) > 12 && Integer.parseInt(str[0]) < 18) {
                        end++;
                    }
                }
                cellDatas.add(new TableCellData(response.classTimes.get(j).getName()+"\n"+response.classTimes.get(j).getBeginTime()+" ~ "+response.classTimes.get(j).getEndTime(),j+1,1));
            }else {
                cellDatas.add(new TableCellData(response.classTimes.get(j).getName(), j + 1, 1));
            }

            //课程内容
            for(int z = 0 ;z < 5;z++){
                cellDatas.add(new TableCellData(response.dayInfos.get(z).get(j).getCourseName()+"\n"+response.dayInfos.get(z).get(j).getTeacherName(),j+1,z+2,response.dayInfos.get(z).get(j)));
            }
        }

        if(start > 0 && end > 0) {
            cellDatas.add(new TableCellData("上午", 1, 0, start, 1));
            cellDatas.add(new TableCellData("下午", start + 1, 0, end, 1));
            if ((start + end) < response.classTimes.size()) {
                cellDatas.add(new TableCellData("晚上", start + end + 1, 0, response.classTimes.size() - start - end, 1));
            }
        }else{
            cellDatas.add(new TableCellData("上午",1,0,4,1));
            cellDatas.add(new TableCellData("下午",5,0,4,1));
            if(response.classTimes.size() > 8) cellDatas.add(new TableCellData("晚上", 9, 0));

        }

        return cellDatas;

    }

    private void drawTable(getCourseTableResponse response ) {
        initData();

        SimpleTableDataAdapter dataAdapter = new SimpleTableDataAdapter(context,getTableData(response), 7);
        dataAdapter.setTextSize(24);


        TableHeaderColumnModel columnModel = new TableHeaderColumnModel(columns);
        SimpleTableHeaderAdapter headerAdapter = new SimpleTableHeaderAdapter(context,columnModel);
        headerAdapter.setTextSize(20);

        tableView.setTableAdapter(headerAdapter,dataAdapter);

        tableView.setHeaderElevation(20);
        tableView.setVisibility(View.VISIBLE);

    }


}
